import advViews from "$v/adv"

import {
    isLoginRequest,
    logoutRequest
} from '$a/admin'
export default async (req, res, next) => {

    //路由切换的时候验证登录
    try {
        await isLoginRequest();
    } catch (e) {
        //请求异常后提示用户
        alert("有异常" + e.message)

        //判断是否是权限异常
        if (!e.isLogin) {
            return router.go("/login");
        }
    }


    // res.render(advViews())
    next(advViews({
        subRoute: res.subRoute(),
        url: req.url

    }));
    const oLogout = document.getElementById('logout')
    oLogout.onclick = async function () {
        try {
            await logoutRequest();
            //退出成功 清掉token
            localStorage.removeItem("user_token");
            //跳转到登录页面
            router.go("/login")
        } catch (e) {
            alert("退出登录异常" + e.message)
        }
    }

}